<!-- 登陆 -->
<template>
  <div class="login-register">
    <div class="contain">
      <div class="big-box" :class="{ active: isLogin }">
        <div class="big-contain" v-if="isLogin">
          <div class="btitle">账户登录</div>
          <div class="bform">
            <input
              type="email"
              placeholder="用户名 / 邮箱"
              v-model="form.username"
            />
            <span class="errTips" v-if="emailError">* 邮箱填写错误 *</span>
            <input
              @keyup.enter="login"
              type="password"
              placeholder="密码"
              v-model="form.userpwd"
            />
            <span class="errTips" v-if="emailError">* 密码填写错误 *</span>
          </div>
          <button class="bbutton" @click="login" @keyup.enter="test">
            登录
          </button>
        </div>
        <div class="big-contain" v-else>
          <div class="btitle">创建账户</div>
          <div class="bform">
            <input
              prefix-icon="el-icon-user"
              type="text"
              placeholder="用户名"
              v-model="form.username"
            />
            <span class="errTips" v-if="existed">* 用户名已经存在！ *</span>
            <input
              type="email"
              prefix-icon="el-icon-message"
              placeholder="邮箱"
              v-model="form.useremail"
              @blur="blurText"
            />
            <input
              prefix-icon="el-icon-lock"
              @keyup.enter="register"
              type="password"
              placeholder="密码"
              v-model="form.userpwd"
            />
          </div>
          <button class="bbutton" @click="register">注册</button>
        </div>
      </div>
      <div class="small-box" :class="{ active: isLogin }">
        <div class="small-contain" v-if="isLogin">
          <div class="stitle">你好，朋友!</div>
          <p class="scontent">开始注册您的智能相册</p>
          <button class="sbutton" @click="changeType">注册</button>
        </div>
        <div class="small-contain" v-else>
          <div class="stitle">欢迎回来!</div>
          <p class="scontent">请登录你的账户</p>
          <button class="sbutton" @click="changeType">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
//import axios from "axios"
export default {
  name: "login-register",
  data() {
    return {
      isLogin: true,
      emailError: false,
      passwordError: false,
      existed: false,
      form: {
        username: "",
        useremail: "",
        userpwd: "",
      },
    };
  },
  methods: {
    blurText() {
      const reg = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
      let str = this.form.useremail;
      if (!reg.test(str)) {
        this.$message.warning("Email format error");
      }
      return reg.test(str);
    },
    changeType() {
      this.isLogin = !this.isLogin;
      this.form.username = "";
      this.form.useremail = "";
      this.form.userpwd = "";
    },
    async login() {
      const self = this;
      if (self.form.username != "" && self.form.userpwd != "") {
        let params = {
          loginName: self.form.username,
          password: self.form.userpwd,
        };
        let data = await self.$post("/ucenter/login", params);
        if (data.success == true) {
          self.$store.dispatch("login/storeToken", data.data.token);
          console.log("登陆成功");
          self.$router.replace("/home");
        } else if (data.code == 402) {
          this.$message({
            message: "登陆失败，用户信息不存在\n请注册后登陆",
            showClose: true,
          });
        } else {
          this.$message({
            message: "账户或者密码错误",
            showClose: true,
          });
        }
      } else {
        this.$message({
          type: "error",
          message: "填写不能为空！",
          showClose: true,
        });
      }
    },
    async register() {
      let self = this;
      if (
        self.form.username != "" &&
        self.form.useremail != "" &&
        self.form.userpwd != ""
      ) {
        if (self.blurText(self.form.useremail)) {
          let params = {
            email: self.form.useremail,
            loginName: self.form.username,
            password: self.form.userpwd,
            phone: null,
            realName: "uesr",
          };
          let data = await self.$post("/ucenter/register", params);
          if (data.success == true && data.message == "注册成功") {
            this.$notify({
              title: "注册成功，欢迎您！",
              type: "success",
              duration: 4000,
            });
            self.isLogin = true;
          } else if (data.success == false && data.code == 402) {
            this.$notify({
              title: "该账户信息已存在",
              type: "warning",
              duration: 4000,
            });
          } else {
            this.$notify({
              title: "注册失败，请重试",
              type: "error",
              duration: 4000,
            });
          }
        } else {
          this.$message.warning("邮箱格式错误");
        }
      } else {
        this.$message({
          type: "error",
          message: "填写不能为空！",
          showClose: true,
        });
      }
    },
  },
};
</script>

<style scoped="scoped">
.login-register {
  width: 100vw;
  height: 100vh;
  box-sizing: border-box;
}
.contain {
  width: 60%;
  height: 60%;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 20px;
  box-shadow: 0 0 3px #f0f0f0, 0 0 6px #f0f0f0;
}
.big-box {
  width: 70%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 30%;
  transform: translateX(0%);
  transition: all 1s;
}
.big-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.btitle {
  font-size: 1.5em;
  font-weight: bold;
  color: rgb(57, 167, 176);
}
.bform {
  width: 100%;
  height: 40%;
  padding: 2em 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.bform .errTips {
  display: block;
  width: 50%;
  text-align: left;
  color: red;
  font-size: 0.7em;
  margin-left: 1em;
}
.bform input {
  width: 50%;
  height: 30px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
}
.el-input {
  width: 50%;
  height: 30px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
}
.bbutton {
  width: 20%;
  height: 40px;
  border-radius: 24px;
  border: none;
  outline: none;
  background-color: rgb(57, 167, 176);
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}
.small-box {
  width: 30%;
  height: 100%;
  background: linear-gradient(135deg, rgb(57, 167, 176), rgb(56, 183, 145));
  position: absolute;
  top: 0;
  left: 0;
  transform: translateX(0%);
  transition: all 1s;
  border-top-left-radius: inherit;
  border-bottom-left-radius: inherit;
}
.small-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.stitle {
  font-size: 1.5em;
  font-weight: bold;
  color: #fff;
}
.scontent {
  font-size: 0.8em;
  color: #fff;
  text-align: center;
  padding: 2em 4em;
  line-height: 1.7em;
}
.sbutton {
  width: 60%;
  height: 40px;
  border-radius: 24px;
  border: 1px solid #fff;
  outline: none;
  background-color: transparent;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}

.big-box.active {
  left: 0;
  transition: all 0.5s;
}
.small-box.active {
  left: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-top-right-radius: inherit;
  border-bottom-right-radius: inherit;
  transform: translateX(-100%);
  transition: all 1s;
}
</style>

